package com.flying.flyingchessgame;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 飞行棋游戏应用程序启动类
 * <p>
 * 应用程序的入口点，使用Spring Boot框架构建。
 * 启动Web服务器、数据库连接池、安全配置等。
 * </p>
 * 
 * @author flying-chess
 * @version 1.0
 */
@SpringBootApplication
@MapperScan("com.flying.flyingchessgame.mapper")
public class FlyingChessGameApplication {

    private static final Logger logger = LoggerFactory.getLogger(FlyingChessGameApplication.class);

    /**
     * 应用程序主方法
     * <p>
     * 启动Spring Boot应用程序，并输出访问URL信息。
     * </p>
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        ConfigurableApplicationContext application = SpringApplication.run(FlyingChessGameApplication.class, args);
        
        Environment env = application.getEnvironment();
        String protocol = "http";
        String port = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path", "");
        
        try {
            String hostAddress = InetAddress.getLocalHost().getHostAddress();
            logger.info("\n----------------------------------------------------------\n\t" +
                    "应用程序 '{}' 已启动! 访问URL:\n\t" +
                    "本地: \t{}://localhost:{}{}\n\t" +
                    "外部: \t{}://{}:{}{}\n\t" +
                    "环境: \t{}\n" +
                    "----------------------------------------------------------",
                    env.getProperty("spring.application.name", "飞行棋游戏"),
                    protocol,
                    port,
                    contextPath,
                    protocol,
                    hostAddress,
                    port,
                    contextPath,
                    env.getActiveProfiles().length == 0 ? "默认" : String.join(", ", env.getActiveProfiles()));
        } catch (UnknownHostException e) {
            logger.error("无法确定主机地址", e);
        }
    }
}
